package org.rendao.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.rendao.biz.data.SecondIndexDO;
import org.rendao.biz.model.SecondIndex;
import org.rendao.biz.service.SecondIndexService;
import org.rendao.constant.ConstantDefinition;
import org.rendao.dao.IGenericDao;
import org.rendao.exception.TBFError;
import org.rendao.exception.TBFException;
import org.rendao.utils.AssembleUtils;
import org.rendao.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SecondIndexServiceImpl implements SecondIndexService {

	private IGenericDao<SecondIndexDO> dao;

	@Autowired
	public void setDao(IGenericDao<SecondIndexDO> daoToSet) {
		dao = daoToSet;
		dao.setClazz(SecondIndexDO.class);
	}
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	@Override
	public SecondIndex getById(String id) {
		SecondIndexDO secondIndexDO = dao.getById(id);
		return AssembleUtils.shadowCopy(secondIndexDO, SecondIndex.class);
	}

	@Override
	public SecondIndex saveOrUpdate(SecondIndex secondIndex) {
		SecondIndexDO secondIndexDO = AssembleUtils.shadowCopy(secondIndex, SecondIndexDO.class);
		SecondIndexDO result = null;
		if(StringUtils.isNullOrEmpty(secondIndex.getId())) {
			result = dao.save(secondIndexDO);
		}
		else {
			result = dao.update(secondIndexDO);
		}
		return AssembleUtils.shadowCopy(result, SecondIndex.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SecondIndex> getByFirstIndex(String firstIndexId) {
		String hql = "from SecondIndexDO s where s.firstIndexId = :firstIndexId and " +
				"s.isDeleted = :isDeleted";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("firstIndexId", firstIndexId);
		query.setParameter("isDeleted", ConstantDefinition.IS_DELETED_FALSE);
		List<SecondIndexDO> list = query.list();
		List<SecondIndex> result = AssembleUtils.shadowCopyList(list, SecondIndex.class);
		return result;
	}

	@Override
	public List<SecondIndex> getAll() {
		List<SecondIndexDO> list = dao.getAllAvailable();
		List<SecondIndex> result = AssembleUtils.shadowCopyList(list, SecondIndex.class);
		return result;
	}

	@Override
	public int deleteById(String id) {
		String sql = "select count(*) from question where second_index_id = :second_index_id" +
				" and is_deleted = :is_deleted";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("second_index_id", id);
		params.put("is_deleted", ConstantDefinition.IS_DELETED_FALSE);
		int count = jdbcTemplate.queryForInt(sql, params);
		if(count > 0) {
			throw new TBFException(TBFError.ASSOCIATION_DELETE_DENY);
		}
		return dao.softDelete(id);
	}

}
